package com.geek.factory.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geek.factory.dto.ProductionScheduleDTO;
import com.geek.factory.entity.ProductionSchedule;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ProductionScheduleMapper  extends BaseMapper<ProductionSchedule> {

    @Select("<script>" +
            "SELECT " +
            "  ps.*, " +
            "  pp.plan_code AS productionPlanCode, " +
            "  pl.line_name AS lineName " +
            "FROM production_schedule ps " +
            "LEFT JOIN production_plan pp ON ps.production_plan_id = pp.id " +
            "LEFT JOIN product_line pl ON ps.product_line_id = pl.id " +
            "<where>" +
            "  <if test='productionPlanId != null'>" +
            "    AND ps.production_plan_id = #{productionPlanId}" +
            "  </if>" +
            "  <if test='productLineId != null'>" +
            "    AND ps.product_line_id = #{productLineId}" +
            "  </if>" +
            "</where>" +
            "ORDER BY ps.start_time DESC" +
            "</script>")
    List<ProductionScheduleDTO> selectWithNames(ProductionScheduleDTO dto);

    @Select("SELECT " +
            "ps.*, " +
            "pp.plan_code AS productionPlanCode, " +
            "pl.line_name AS productLineName " +
            "FROM production_schedule ps " +
            "LEFT JOIN production_plan pp ON ps.production_plan_id = pp.id " +
            "LEFT JOIN product_line pl ON ps.product_line_id = pl.id " +
            "WHERE ps.shift_name = #{shift}")
    List<ProductionScheduleDTO> selectByShiftWithNames(@Param("shift") String shift);

}
